Put a link instead of a field

Put a link instead of a field

ClaudioGSClaudioGS Posts: 29Questions: 1Answers: 0

Hello good morning.
I have the following code:

 tblDocumentos = $('#tblDocumentos').DataTable({
    ajax: {
      url: base_url + "/Documentos/listar",
      dataSrc: ''
    },
    columns: [
      { 'data': 'fecha'},
      { 'data': 'numero' },
      { 'data': 'tipo' },
      { 'data': 'archivo' },
      {'data': null,
        render: function ( data, type, row ) {
        return row.nombre + ' -- (' + row.rut + ')';}},
      { 'data': 'estado'},
      { 'data': 'acciones'},
    ],
    language: {
      "url": "//cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json"
    },
    dom,
    buttons
  }); 

Instead of the file field, I want to put an icon, with a link to the file.

Is it possible to do that?

A cordial greeting from Valparaíso - Chile````

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,188Questions: 26Answers: 4,925

    There is a link code example in the columns.render docs. Also see this example.

    Kevin

  • ClaudioGSClaudioGS Posts: 29Questions: 1Answers: 0
    edited July 20

    Gracias por la información, ya lo pude lograr

  • ClaudioGSClaudioGS Posts: 29Questions: 1Answers: 0

    I already have part of what I need, my question now is, how can I make a conditional.
    if (empty(file){ leave the field empty, do not put the icon with the link }

    { 'data': 'archivo', render: function (data){ return '<div class="text-center"><a href="/doctos2/Assets/uploads/' + data + '"><box-icon name="link" size="sm"></box-icon></a></div>' } },

  • kthorngrenkthorngren Posts: 21,188Questions: 26Answers: 4,925

    Its hard to say specifically without understanding your data but use an if statement in the columns.render function. The key is to make sure to always return something. For example:

    { 'data': 'archivo',
            render: function (data){
              if (empty(file){
                return "";
              } else {
                return '<div class="text-center"><a href="/doctos2/Assets/uploads/' + data + '"><box-icon name="link" size="sm"></box-icon></a></div>'
              }
            }
           },
    

    Replace empty(file) with whatever code checks your data for an empty file. Possibly use the row parameter to access another data point in the row.

    Kevin

  • ClaudioGSClaudioGS Posts: 29Questions: 1Answers: 0

    I did the following:
    { 'data': 'archivo', render: function (data){ if(data != ''){ return '<div class="text-center"><a href="/doctos2/Assets/uploads/' + data + '"><box-icon name="link" size="sm"></box-icon></a></div>' } } },

    the following message appears:

    When I press the "Accept" button twice, the data appears as I need it, that is, the records that do not have a file inserted, the icon does not appear, when I check the console there are no errors, nor on the network, therefore I do not know what it could be . reading the documentation of the link in the message, I can't find the reason.

    Please can you help me.

  • kthorngrenkthorngren Posts: 21,188Questions: 26Answers: 4,925
    edited July 21 Answer ✓

    That error is why I said you always need to return something. In my code snippet there is a return statement when the if is true and in the else clause. You need to do the same.

    Kevin

  • ClaudioGSClaudioGS Posts: 29Questions: 1Answers: 0

    I solved it by adding an else: leaving it as follows:
    { 'data': 'archivo', render: function (data){ if(data != ''){ return '<div class="text-center"><a href="/doctos2/Assets/uploads/' + data + '"><box-icon name="link" size="sm"></box-icon></a></div>' } else { return '<div class="text-center"><img src="/doctos2/Assets/uploads/default.png" width=70></div>' } } },

Sign In or Register to comment.