簡體   English   中英

如何在Select2下拉過濾器中使用jQuery解碼JSON Unicode

[英]How can I decode JSON Unicode using jQuery in a Select2 dropdown filter

我正在使用Select2 JS和Datatables JS。 數據采用JSON格式。 一個值的數據應顯示為Animal & Veterinary 在JSON中它顯示為Animal \& Veterinary Animal \& Veterinary Select2過濾器將其顯示為Animal & Veterinary Animal & Veterinary 如何在下面的JS中添加一個解碼Unicode的函數? 下面是一個可以工作的函數,但我不知道如何將它添加到JS下面。

var title = 'Animal & Veterinary';
function stringToSlug (title) {
  return title.toLowerCase().trim()
  .replace(/&/g, 'and')
}

下面是腳本。 這是我想將函數傳遞給過濾器的地方。 “select2config”

jQuery( document ).ready( function($) {
      'use strict';

      // Check condition first if the table class exists then run this init.
      if ($('.lcds-datatable--advisory-committee-materials').length > 0) {

      let pageClass = function () {
          let el = $( 'ul.pagination' ).addClass('pagination-sm');
      }

      // define order of table's control element
      let domStyling = "<'row'<'col-sm-12'lB>>" +
        "<'row'<'col-sm-12'tr>>" +
        "<'row'<'col-sm-5'i><'col-sm-7'p>>";

      var otable9 = $('table.lcds-datatable--advisory-committee-materials').DataTable( {
        // Sort of first date column descending.
        order: [[0, 'desc']],
        deferRender: true,
        deferLoading: 50,
        dom: domStyling,
        ajax: {
                "url": "/datatables-json/advisory-committee-materials-json",
                //"url": "/sites/default/files/actest1.json",
                "dataSrc": ""
            },
        processing: true,
        columns: [
                { "data": "field_publish_date" }, // publish_date 0
                { "data": "title" }, // node title summary 1
                { "data": "field_site_structure" } // site_structure Committee/Topic 2

            ],
        columnDefs: [
                {
                    "type": "date",
                    "targets": [ 0 ]
                }
            ],
        pageLength: 10,
        searching: true,
        autoWidth: false,
        responsive: true,
        lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
        buttons: [
          {
            extend: 'excel',
            text: 'Export Excel',
            exportOptions: {
                        columns: [ 0, 1, 2 ]
                    }

          }
        ],
        initComplete: pageClass,
        drawCallback: pageClass
      }); // end datatable

      // config and initialize filters

      let select2config = {
        maximumSelectionLength: 0,
        minimumInputLength: 0,
        tags: true,
        selectOnClose: true,
        theme: "bootstrap",
      }

      let search = $( '#lcds-datatable-filter--search' ).lcdsTableFilter({
        table: otable9,
        type: 'search'
      });

      let clear = $( '#lcds-datatable-filter--clear' ).lcdsTableFilter({
        table: otable9,
        type: 'clear'
      });

      // wait for ajax call to complete to load column data load
      $('table.lcds-datatable--advisory-committee-materials').on( 'init.dt', function() {

        let committee = $( '#lcds-datatable-filter--committee-topic' ).lcdsTableFilter({
          column: 2,
          table: otable9,
          select2Options: select2config,
          type: 'select',
          dataType: 'datatable'
        });
      })

    }}); // end ready function and condition if the table class exists.

我能夠傳遞一個函數來改變JSON的輸出,所以現在過濾器顯示“Animal&Veterinary”而不是“Animal&Veterinary”通過添加以下內容。 過濾器現在顯示動物和獸醫,但在過濾器中選擇此值與數據表中的值不匹配?

let select2config = {
maximumSelectionLength: 0,
minimumInputLength: 0,
tags: true,
selectOnClose: true,
theme: "bootstrap",
escapeMarkup: function (text) { return text; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM